
import  pymysql
from pymysql.cursors import  DictCursor

CONFIG = {
    'host': 'locahost',
    'port': 3306,
    'user': 'root',
    'pwd': 'root',
    'db': 'dhd',
    'charset': 'utf8',
    'cursorclass': DictCursor
}

class DB():
    def __init__(self):
        self.conn = pymysql.Connect(**CONFIG)

    def __enter__(self):
        return self.conn.cursor()

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type:
            self.conn.rollback()
        else:
            self.conn.commit()
    def close(self):
        if self.conn:
            self.conn.close()
            self.conn = None

